home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / smail-3.1.28 / src / dys.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-11  |  3.1 KB  |  105 lines

  1. /* @(#)src/dys.h    1.5 7/11/92 11:48:55 */
  2.  
  3. /*
  4.  *    Copyright (C) 1987, 1988 Ronald S. Karr and Landon Curt Noll
  5.  *    Copyright (C) 1992  Ronald S. Karr
  6.  * 
  7.  * See the file COPYING, distributed with smail, for restriction
  8.  * and warranty information.
  9.  */
  10.  
  11. /*
  12.  * dys.h:
  13.  *    macros for dynamic string region functions.
  14.  *
  15.  *    The macros in this file provide a simple method of
  16.  *    building strings in an environment where the final
  17.  *    length requirements are not known.  Thus, these
  18.  *    macros automatically support growing of strings
  19.  *    with xrealloc() when the current allocation is deemed
  20.  *    insufficient.
  21.  *
  22.  *    All the macros take, as the first three arguments,
  23.  *    a character pointer, an index and an allocation size.
  24.  *    The index is an offset from the character pointer
  25.  *    to the current location being copied into and the
  26.  *    allocation size is the current limit for the index,
  27.  *    beyond which a call to xrealloc() is required.
  28.  *    The specific macros are described when they are
  29.  *    defined
  30.  */
  31.  
  32. /* type for a dynamic string region */
  33. struct str {
  34.     char *p;                /* xmalloc'd text region */
  35.     unsigned int i;            /* index into text region */
  36.     unsigned int a;            /* current allocation size */
  37. };
  38.  
  39.  
  40. /* STR_CHECK - call X_CHECK (from alloc.h) for a string */
  41. #define STR_CHECK(sp) X_CHECK((sp)->p)
  42.  
  43. /* STR_BUMP - the basic quantum of allocation space */
  44. #define STR_BUMP    64
  45.  
  46. /*
  47.  * STR_INIT - initialize the variables for a dynamic string region
  48.  * this macro should be called with the variables to be passed to
  49.  * other macros in this package before those other macros are used
  50.  */
  51. #define STR_INIT(sp)                    \
  52.     (((sp)->a = STR_BUMP - sizeof(long)),        \
  53.      ((sp)->i = 0),                    \
  54.      ((sp)->p = xmalloc((sp)->a)))
  55.  
  56. /*
  57.  * STR_NEXT - allow access to the next character in a dynamic string
  58.  * region, growing the region if no successor character currently
  59.  * is allocated.  This can be used in the form:
  60.  *
  61.  *    STR_NEXT(p, character expression);
  62.  *
  63.  * to load successive characters into the string.
  64.  */
  65. #define STR_NEXT(sp, c)                 \
  66.     {                        \
  67.         if ((sp)->i >= (sp)->a) {            \
  68.         (sp)->a += STR_BUMP;            \
  69.         (sp)->p = xrealloc((sp)->p, (sp)->a);    \
  70.         }                        \
  71.         (sp)->p[(sp)->i++] = (c);            \
  72.     }
  73.  
  74. /*
  75.  * STR_CAT - concatenate a string onto the end of a dynamic string
  76.  * region, growing as necessary.
  77.  *
  78.  * This is now implemented as a function in string.c.
  79.  */
  80. #define STR_CAT(sp, s)     str_cat((sp), (s))
  81.  
  82. /*
  83.  * STR_DONE - finish building a dynamic string region.  This is not
  84.  * required, though it will xrealloc a region to minimum length, which
  85.  * may be useful if xmalloc and xrealloc call something besides the
  86.  * stock malloc and realloc functions.
  87.  */
  88. #define STR_DONE(sp)    ((sp)->p = xrealloc((sp)->p, (sp)->i + 1), \
  89.              (sp)->a = (sp)->i + 1)
  90.  
  91. /*
  92.  * STR_FREE - free a region, returning its storage to the free pool
  93.  */
  94. #define STR_FREE(sp)    (xfree((sp)->p))
  95.  
  96. /*
  97.  * STR_ALIGN - if region index is not aligned add bytes to align it.
  98.  */
  99. #define STR_ALIGN(sp)    { while ((sp)->i%BYTES_PER_ALIGN) STR_NEXT((sp), 0); }
  100.  
  101. /*
  102.  * COPY_STRING - copy a C-style string to a new xmalloc'd region
  103.  */
  104. #define COPY_STRING(s)    (strcpy(xmalloc(strlen((s)) + 1), (s)))
  105.